<?php

namespace app\common\service;

use app\apiadmin\model\files\FilesSetModel;
use app\apiadmin\service\files\FilesService;
use app\apiadmin\service\files\UploadCOSService;
use app\common\utils\Res;
use think\facade\Filesystem;
use think\facade\Request;

class UploadService
{
    
    public static function uploads($file, $by,$uid, $fileType='', $setup=[],$projectType = 'img',$source=0)
    {
        $fileSet = FilesSetModel::findByid();
        if (empty($file)) {
            return Res::error("上传文件错误");
        }
        $extension = $file->getOriginalExtension();
        $size = $file->getSize();
        if (!empty($setup)) {
            if (!in_array($extension, explode(',', $setup['suffix']))) {
                return Res::error("[" . $extension . "]上传文件失败，不允许上传的文件类型");
            }
            if ($size > $setup['size']) {
                return Res::error('上传文件失败，文件超过上传限制');
            }
        } else {
            if (!in_array($extension, explode(',', $fileSet['allowExts']))) {
                return Res::error("[" . $extension . "]上传文件失败，不允许上传的文件类型");
            }
            if ($size > $fileSet['fileSize']) {
                return Res::error('上传文件失败，文件超过上传限制');
            }
        }
        
        $getSaveName = str_replace("\\", "/", $file->getOriginalName());
        $type = 4000;
        if (!empty($fileType)) {
            //判断上传文件类型 $fileType 0图片 1文件 2视频 3音频 4其他
            if(!in_array($fileType,[0,1,2,3])){
                return Res::error('上传文件失败，文件类型错误');
            }
            //图片
            if($fileType=='0'){
                $img = "bmp,jpg,jpeg,png,tiff,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf";
                if (strpos($img, $extension) === false) {
                    return Res::error('上传文件失败，图片格式错误');
                }
            }
            //文件
            if($fileType=='1'){
                $img = "txt,doc,docx,xls,htm,html,jsp,rtf,wpd,pdf,ppt";
                if (strpos($img, $extension) === false) {
                    return Res::error('上传文件失败，文件格式错误');
                }
            }
            //视频
            if($fileType=='2'){
                $img = "mp4,avi,mov,wmv,asf,navi,3gp,mkv,f4v,rmvb,webm";
                if (strpos($img, $extension) === false) {
                    return Res::error('上传文件失败，视频格式错误');
                }
            }
            //音频
            if($fileType=='3'){
                $img = "mp3,amr";
                if (strpos($img, $extension) === false) {
                    return Res::error('上传文件失败，音频格式错误');
                }
            }
            $type = $fileType;
        } else {
            // 自动判断文件类型
            $img = "bmp,jpg,jpeg,png,tiff,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf";
            if (strpos($img, $extension) != false) {
                $type = 0;
            }
            $txt = "txt,doc,docx,xls,htm,html,jsp,rtf,wpd,pdf,ppt";
            if (strpos($txt, $extension) != false) {
                $type = 1;
            }
            $video = "mp4,avi,mov,wmv,asf,navi,3gp,mkv,f4v,rmvb,webm";
            if (strpos($video, $extension) != false) {
                $type = 2;
            }
            $audio = "mp3,amr";
            if (strpos($audio, $extension) != false) {
                $type = 3;
            }
        }
        
        if ($fileSet['files_ares'] == 3) {
            $cosFileService = new UploadCOSService();
            return $cosFileService->ProcessFile($extension, $file, $type, $by,$projectType,$uid,$source);

        }
        return UploadService::local($extension, $file, $type, $by,$source);

    }

    /**
     * @param $extension
     * @param $file
     * 本地存储
     */
    public static function local($extension, $file, $type, $by,$source)
    {

        $fileName = Filesystem::disk('public')->putFile($extension, $file, 'uniqid');
        $sname = str_replace("\\", "/", $fileName);
        $data = [
            'name' => substr($sname, strripos($sname, "/") + 1),
            'f_type' => $extension,
            'type' => $type,
            'urls' => '/storage/' . $fileName,
            'creat_time' => date('Y-m-d H:i:s', time()),
            'creat_by' => $by,
            'http_urls' => Request::domain() . '/storage/' . $fileName,
            'oss' => '本地',
            'source'=>$source,
        ];
        $result = FilesService::add($data);
        return $result;
    }

}
